﻿//
// Include COM --> SASWorkspaceManager 1.1 Type Library
// Include COM --> SAS: Integrated Object Model (IOM) (SAS System 9.1) Type Library 
// Include COM --> SASIOMCommon: Contains common interfaces for IOM programming. (SAS System 9.1.3) Type Library 

using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using SASWorkspaceManager;
using System.IO;

namespace SasPlugIn.SASCli
{
    public class SasClient
    {
        #region Internal Members

        ServerDef sasServerDef;
        SAS.Workspace sasWorkSpace;
        WorkspaceManager sasWSMgr;

        private IomProperties configProperties;

        #endregion

        public SasClient(IomProperties sasConfig)
        {
            configProperties = sasConfig;
        }

        /// <summary>
        /// Create SAS Workspace object instance according to server definitions
        /// received in C'tor.
        /// </summary>
        /// <exception cref="System.ApplicationException">Thrown when an error received
        /// from the CreateWorkSpaceByServer SAS Client API call.</exception>
        public string CreateWorkspace()
        {
            string errString = string.Empty ;

            // Create only once
            if (sasWorkSpace == null)
            {
                sasServerDef = new ServerDef();
                sasServerDef.Port = configProperties.ServerPort;
                sasServerDef.Protocol = SASWorkspaceManager.Protocols.ProtocolBridge;
                sasServerDef.MachineDNSName = configProperties.DnsName;
                sasServerDef.Name = configProperties.ServerDefName;

                sasWSMgr = new SASWorkspaceManager.WorkspaceManager();

                sasWorkSpace = (SAS.Workspace)sasWSMgr.Workspaces.CreateWorkspaceByServer(configProperties.WorkspaceName,
                    SASWorkspaceManager.Visibility.VisibilityNone,
                    sasServerDef,
                    configProperties.LoginName,
                    Decrypt(configProperties.CryptedPwd),
                    out errString);

            }
            return errString;
        }

        /// <summary>
        /// If we don't close SAS, the SAS process may stay around forever
        /// </summary>
        public void Close()
        {
            if (sasWorkSpace != null)
            {
                try
                {
                    sasWSMgr.Workspaces.RemoveWorkspace(sasWorkSpace);
                    sasWorkSpace.Close();
                }
                catch { }
           }
       }

        /// <summary>
        /// TEST SAS Access by requesting SAS procedures Options description.
        /// </summary>
        /// <returns>A string holding the whole description information.</returns>
        public string TestProcOptions()
        {
            string logBuffer = string.Empty;

            CreateWorkspace();

            // This is how to request options description
            sasWorkSpace.LanguageService.Submit("proc options;run;");
            logBuffer = sasWorkSpace.LanguageService.FlushLog(100000);


            // Make better presentation to Windows environment
            logBuffer = logBuffer.Replace("\n\n", "\n");
            logBuffer = logBuffer.Replace("\n", "\r\n");
            Debug.WriteLine(logBuffer);

            return logBuffer;
        }

        private string Decrypt(string crypted)
        {
            return crypted;
        }

        public string CreateTest(string code)
        {
            CreateWorkspace();
            SAS.ILanguageService ls = sasWorkSpace.LanguageService;

            //There needs to be a directory where you place your stored procedures. Here that directory is referenced
            //   as StoredProcedures
            ls.Submit(code);

            string logBuffer = sasWorkSpace.LanguageService.FlushLog(1000000);


            // Make better presentation to Windows environment
            logBuffer = logBuffer.Replace("\n\n", "\n");
            logBuffer = logBuffer.Replace("\n", "\r\n");
            Debug.WriteLine(logBuffer);

            return logBuffer;
        }

    }
   
}
